package com.lxk.openapi.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lxk.openapi.config.LxkOpenApiProperties;
import com.lxk.openapi.constants.LxkOpenApiConstant;
import com.lxk.openapi.model.*;
import com.lxk.openapi.response.Idcard.IdCardResult;
import com.lxk.openapi.response.express.QueryExpressResult;
import com.lxk.openapi.response.face.FaceResult;
import com.lxk.openapi.response.free.IpInfoResult;
import com.lxk.openapi.response.free.IpResult;
import com.lxk.openapi.response.geo.AddressGeoResult;
import com.lxk.openapi.response.record.CommonInfoCountResult;
import com.lxk.openapi.response.record.UserPackageRecordResult;
import com.lxk.openapi.response.sms.SendMesgResult;
import com.lxk.openapi.response.words.VerifyWordsResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LxkOpenApiService {

    private static final String HTTPS_URL_PREFIX = "http://openapi.lianxianke.cn/lxk-api/";
    @Autowired
    private LxkOpenApiProperties lxkOpenApiProperties;

    private String getHttpsUrl(String openServiceApi) {
        return String.format("%s%s", HTTPS_URL_PREFIX, openServiceApi);
    }

    private String getHttpsPageURL(String openServiceApi, Integer pageSize, Integer pageNum, String orderByColumn, String isAsc) {
        return String.format("%s%s?pageSize=%s&pageNum=%s&orderByColumn=%s&isAsc=%s",
            HTTPS_URL_PREFIX, openServiceApi, pageSize, pageNum, orderByColumn, isAsc);
    }

    /**
     * 物流查询 URL
     *
     * @return
     */
    private String getQueryExpressURL() {
        return getHttpsUrl(LxkOpenApiConstant.ExpressManage.QUERY_EXPRESS);
    }

    /**
     * 查询物流消耗记录 URL
     *
     * @return
     */
    private String getExpressRecordURL(Integer pageSize, Integer pageNum, String orderByColumn, String isAsc) {
        return getHttpsPageURL(LxkOpenApiConstant.ExpressManage.EXPRESS_RECORD, pageSize, pageNum, orderByColumn, isAsc);
    }

    /**
     * 物流套餐数量 URL
     *
     * @return
     */
    private String getExpressInfoCountURL() {
        return getHttpsUrl(LxkOpenApiConstant.ExpressManage.EXPRESS_INFO_COUNT);
    }

    /**
     * 发送短信 URL
     *
     * @return
     */
    private String getSmsSendMesssageURL() {
        return getHttpsUrl(LxkOpenApiConstant.SmsManage.SENDSMS);
    }

    private String getSmsSendYxMesssageURL() {
        return getHttpsUrl(LxkOpenApiConstant.SmsManage.SEND_YX_SMS);
    }

    /**
     * 查询短信消耗记录 URL
     *
     * @return
     */
    private String getSmsRecordURL(Integer pageSize, Integer pageNum, String orderByColumn, String isAsc) {
        return getHttpsPageURL(LxkOpenApiConstant.SmsManage.SMS_RECORD, pageSize, pageNum, orderByColumn, isAsc);
    }

    /**
     * 短信套餐数量 URL
     *
     * @return
     */
    private String getSmsInfoCountURL() {
        return getHttpsUrl(LxkOpenApiConstant.SmsManage.SMS_INFO_COUNT);
    }

    /**
     * 经纬度解析地址 URL
     *
     * @return
     */
    private String getParseLatlngURL() {
        return getHttpsUrl(LxkOpenApiConstant.GeoManage.PARSE_LATLNG);
    }

    /**
     * 地址解析经纬度 URL
     *
     * @return
     */
    private String getParseAddressURL() {
        return getHttpsUrl(LxkOpenApiConstant.GeoManage.PARSE_ADDRESS);
    }

    /**
     * GEO消耗记录 URL
     *
     * @return
     */
    private String getGeoRecordURL(Integer pageSize, Integer pageNum, String orderByColumn, String isAsc) {
        return getHttpsPageURL(LxkOpenApiConstant.GeoManage.GEO_RECORD, pageSize, pageNum, orderByColumn, isAsc);
    }

    /**
     * GEO套餐数量 URL
     *
     * @return
     */
    private String getGeoInfoCountURL() {
        return getHttpsUrl(LxkOpenApiConstant.GeoManage.GEO_INFO_COUNT);
    }

    /**
     * 金融级人脸对比验证 URL
     *
     * @return
     */
    private String getFaceFaceURL() {
        return getHttpsUrl(LxkOpenApiConstant.FaceManage.FACE_V1_FACE);
    }

    /**
     * 金融级人脸验证加强版 URL
     *
     * @return
     */
    private String getFacePlusURL() {
        return getHttpsUrl(LxkOpenApiConstant.FaceManage.FACE_V1_FACEPLUS);
    }

    /**
     * 普通人脸对比验证 URL
     *
     * @return
     */
    private String getFaceCompareURL() {
        return getHttpsUrl(LxkOpenApiConstant.FaceManage.FACE_V1_FACECOMPARE);
    }

    /**
     * 查询金融人脸消耗记录 URL
     *
     * @return
     */
    private String getFacePlusRecordURL(Integer pageSize, Integer pageNum, String orderByColumn, String isAsc) {
        return getHttpsPageURL(LxkOpenApiConstant.FaceManage.FACE_V1_FACEPLUS_RECORD, pageSize, pageNum, orderByColumn, isAsc);
    }

    /**
     * 金融级人脸套餐数量 URL
     *
     * @return
     */
    private String getFacePlusInfoCountURL() {
        return getHttpsUrl(LxkOpenApiConstant.FaceManage.FACEPLUS_INFO_COUNT);
    }

    /**
     * 实名认证 URL
     *
     * @return
     */
    private String getIdVerifyIdCardURL() {
        return getHttpsUrl(LxkOpenApiConstant.IdCardManage.VERIFY_IDCARD);
    }

    /**
     * 实名消耗记录 URL
     *
     * @return
     */
    private String getIdCardRecordURL(Integer pageSize, Integer pageNum, String orderByColumn, String isAsc) {
        return getHttpsPageURL(LxkOpenApiConstant.IdCardManage.IDCARD_RECORD, pageSize, pageNum, orderByColumn, isAsc);
    }

    /**
     * 实名套餐数量 URL
     *
     * @return
     */
    private String getIdCardInfoCountURL() {
        return getHttpsUrl(LxkOpenApiConstant.IdCardManage.IDCARD_INFO_COUNT);
    }

    /**
     * 敏感词识别接口 URL
     *
     * @return
     */
    private String getVerifyWordsURL() {
        return getHttpsUrl(LxkOpenApiConstant.SensitiveWordsManage.VERIFY_WORDS);
    }

    /**
     * 敏感词消耗记录 URL
     *
     * @return
     */
    private String getWordsRecordURL(Integer pageSize, Integer pageNum, String orderByColumn, String isAsc) {
        return getHttpsPageURL(LxkOpenApiConstant.SensitiveWordsManage.WORDS_RECORD, pageSize, pageNum, orderByColumn, isAsc);
    }

    /**
     * 敏感词套餐数量 URL
     *
     * @return
     */
    private String getWordsInfoCountURL() {
        return getHttpsUrl(LxkOpenApiConstant.SensitiveWordsManage.WORDS_INFO_COUNT);
    }

    private String getApiIpUrl() {
        return getHttpsUrl(LxkOpenApiConstant.FreeAPI.IP_URL);
    }

    /**
     * 查询物流
     *
     * @param queryExpressRequest
     * @return
     */
    public QueryExpressResult queryExpress(QueryExpressRequest queryExpressRequest) {
        String httpsUrl = getQueryExpressURL();
        JSONObject jsonObject = new JSONObject();
        if (StrUtil.isNotBlank(queryExpressRequest.getType())) {
            jsonObject.put("type", queryExpressRequest.getType());
        }
        jsonObject.put("no", queryExpressRequest.getNo());
        log.info("查询物流，请求参数：{}", jsonObject);
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, QueryExpressResult.class);
    }

    /**
     * 查询物流消耗记录
     *
     * @param pageQueryRequest
     * @return
     */
    public UserPackageRecordResult expressRecord(PageQueryRequest pageQueryRequest) {
        String httpsUrl = getExpressRecordURL(pageQueryRequest.getPageSize(), pageQueryRequest.getPageNum(), pageQueryRequest.getOrderByColumn(), pageQueryRequest.getIsAsc());
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, UserPackageRecordResult.class);
    }

    /**
     * 物流套餐数量
     *
     * @return
     */
    public CommonInfoCountResult expressInfoCount() {
        String httpsUrl = getExpressInfoCountURL();
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, CommonInfoCountResult.class);
    }

    /**
     * 发送营销短信
     *
     * @param smsSendRequest
     * @return
     */
    public SendMesgResult smsSendYxMesssage(SmsSendRequest smsSendRequest) {
        String httpsUrl = getSmsSendYxMesssageURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobile", smsSendRequest.getMobile());
        jsonObject.put("smsSignId", lxkOpenApiProperties.getSmsSignId());
        jsonObject.put("templateId", smsSendRequest.getTemplateId());
        jsonObject.put("paramMap", smsSendRequest.getParamMap());
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, SendMesgResult.class);
    }

    /**
     * 发送短信
     *
     * @param smsSendRequest
     * @return
     */
    public SendMesgResult smsSendMesssage(SmsSendRequest smsSendRequest) {
        String httpsUrl = getSmsSendMesssageURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobile", smsSendRequest.getMobile());
        jsonObject.put("smsSignId", lxkOpenApiProperties.getSmsSignId());
        jsonObject.put("templateId", smsSendRequest.getTemplateId());
        jsonObject.put("paramMap", smsSendRequest.getParamMap());
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, SendMesgResult.class);
    }

    /**
     * 查询短信消耗记录
     *
     * @param pageQueryRequest
     * @return
     */
    public UserPackageRecordResult smsRecord(PageQueryRequest pageQueryRequest) {
        String httpsUrl = getSmsRecordURL(pageQueryRequest.getPageSize(), pageQueryRequest.getPageNum(), pageQueryRequest.getOrderByColumn(), pageQueryRequest.getIsAsc());
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, UserPackageRecordResult.class);
    }

    /**
     * 短信套餐数量
     *
     * @return
     */
    public CommonInfoCountResult smsInfoCount() {
        String httpsUrl = getSmsInfoCountURL();
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, CommonInfoCountResult.class);
    }

    /**
     * 经纬度解析地址
     *
     * @param geoLatLngRequest
     * @return
     */
    public AddressGeoResult parseLatlng(GeoLatLngRequest geoLatLngRequest) {
        String httpsUrl = getParseLatlngURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("lng", geoLatLngRequest.getLng());
        jsonObject.put("lat", geoLatLngRequest.getLat());
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, AddressGeoResult.class);
    }

    /**
     * 地址解析经纬度
     *
     * @param address 地址
     * @return
     */
    public AddressGeoResult parseAddress(String address) {
        String httpsUrl = getParseAddressURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("address", address);
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, AddressGeoResult.class);
    }

    /**
     * 查询GEO消耗记录
     *
     * @param pageQueryRequest
     * @return
     */
    public UserPackageRecordResult geoRecord(PageQueryRequest pageQueryRequest) {
        String httpsUrl = getGeoRecordURL(pageQueryRequest.getPageSize(), pageQueryRequest.getPageNum(), pageQueryRequest.getOrderByColumn(), pageQueryRequest.getIsAsc());
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, UserPackageRecordResult.class);
    }

    /**
     * GEO套餐数量
     *
     * @return
     */
    public CommonInfoCountResult geoInfoCount() {
        String httpsUrl = getGeoInfoCountURL();
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, CommonInfoCountResult.class);
    }

    /**
     * 金融级人脸对比验证
     *
     * @param faceFaceRequest
     * @return
     */
    public FaceResult faceFace(FaceFaceRequest faceFaceRequest) {
        String httpsUrl = getFaceFaceURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("faceUrl", faceFaceRequest.getFaceUrl());
        jsonObject.put("faceUrl2", faceFaceRequest.getFaceUrlSecond());
        jsonObject.put("idCardNo", faceFaceRequest.getIdCardNo());
        jsonObject.put("name", faceFaceRequest.getName());
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, FaceResult.class);
    }

    /**
     * 金融级人脸验证加强版
     *
     * @param facePlusRequest
     * @return
     */
    public FaceResult facePlus(FacePlusRequest facePlusRequest) {
        String httpsUrl = getFacePlusURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("faceUrl", facePlusRequest.getFaceUrl());
        jsonObject.put("idCardNo", facePlusRequest.getIdCardNo());
        jsonObject.put("name", facePlusRequest.getName());
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, FaceResult.class);
    }

    /**
     * 普通人脸对比验证
     *
     * @param faceCompareRequest
     * @return
     */
    public FaceResult faceCompare(FaceCompareRequest faceCompareRequest) {
        String httpsUrl = getFaceCompareURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("faceUrl", faceCompareRequest.getFaceUrl());
        jsonObject.put("faceUrl2", faceCompareRequest.getFaceUrlSecond());
        if (StrUtil.isNotBlank(faceCompareRequest.getName())) {
            jsonObject.put("name", faceCompareRequest.getName());
        }
        if (StrUtil.isNotBlank(faceCompareRequest.getIdCardNo())) {
            jsonObject.put("idCardNo", faceCompareRequest.getIdCardNo());
        }
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, FaceResult.class);
    }

    /**
     * 查询金融人脸消耗记录
     *
     * @param pageQueryRequest
     * @return
     */
    public UserPackageRecordResult facePlusRecord(PageQueryRequest pageQueryRequest) {
        String httpsUrl = getFacePlusRecordURL(pageQueryRequest.getPageSize(), pageQueryRequest.getPageNum(), pageQueryRequest.getOrderByColumn(), pageQueryRequest.getIsAsc());
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, UserPackageRecordResult.class);
    }

    /**
     * 金融级人脸套餐数量
     *
     * @return
     */
    public CommonInfoCountResult facePlusCount() {
        String httpsUrl = getFacePlusInfoCountURL();
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, CommonInfoCountResult.class);
    }

    /**
     * 实名认证
     *
     * @param verifyIdCardRequest
     * @return
     */
    public IdCardResult verifyIdCard(VerifyIdCardRequest verifyIdCardRequest) {
        String httpsUrl = getIdVerifyIdCardURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("idCardNo", verifyIdCardRequest.getIdCardNo());
        jsonObject.put("name", verifyIdCardRequest.getName());
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, IdCardResult.class);
    }


    /**
     * 实名消耗记录
     *
     * @param pageQueryRequest
     * @return
     */
    public UserPackageRecordResult idCardRecord(PageQueryRequest pageQueryRequest) {
        String httpsUrl = getIdCardRecordURL(pageQueryRequest.getPageSize(), pageQueryRequest.getPageNum(), pageQueryRequest.getOrderByColumn(), pageQueryRequest.getIsAsc());
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, UserPackageRecordResult.class);
    }

    /**
     * 实名套餐数量
     *
     * @return
     */
    public CommonInfoCountResult idCardInfoCount() {
        String httpsUrl = getIdCardInfoCountURL();
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, CommonInfoCountResult.class);
    }

    /**
     * 敏感词识别接口
     *
     * @param verifyWordsRequest
     * @return
     */
    public VerifyWordsResult verifyWords(VerifyWordsRequest verifyWordsRequest) {
        String httpsUrl = getVerifyWordsURL();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content", verifyWordsRequest.getContent());
        jsonObject.put("type", verifyWordsRequest.getType());
        String result = HttpUtil.createPost(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .contentType("application/json")
            .body(jsonObject.toJSONString(0)).execute().body();
        return JSONUtil.toBean(result, VerifyWordsResult.class);
    }

    /**
     * 敏感词消耗记录
     *
     * @param pageQueryRequest
     * @return
     */
    public UserPackageRecordResult wordsRecord(PageQueryRequest pageQueryRequest) {
        String httpsUrl = getWordsRecordURL(pageQueryRequest.getPageSize(), pageQueryRequest.getPageNum(), pageQueryRequest.getOrderByColumn(), pageQueryRequest.getIsAsc());
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, UserPackageRecordResult.class);
    }

    /**
     * 敏感词套餐数量
     *
     * @return
     */
    public CommonInfoCountResult wordsInfoCount() {
        String httpsUrl = getWordsInfoCountURL();
        String result = HttpUtil.createGet(httpsUrl)
            .header("access_key", lxkOpenApiProperties.getLxkAccessKey())
            .header("secret_key", lxkOpenApiProperties.getLxkSecretKey())
            .execute()
            .charset("utf-8")
            .body();
        return JSONUtil.toBean(result, CommonInfoCountResult.class);
    }


    /**
     * 获取当前机器的公网IP
     *
     * @return
     */
    public IpInfoResult getCurrentIp() {
        String httpsUrl = getApiIpUrl();
        String result = HttpUtil.createGet(httpsUrl)
            .execute()
            .charset("utf-8")
            .body();
        IpResult ipResult = JSONUtil.toBean(result, IpResult.class);
        return ipResult.getData();
    }

}
